package com.zwan.oam_rtc.utils.messages;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import com.zwan.oam_rtc.utils.wintcp.ByteTransfer;


public abstract class AbstractStruct implements MsgStruct {
	
	public ByteBuffer getByteBuffer(ByteOrder order){
		ByteBuffer bf = ByteBuffer.allocate(getLength());
		bf.order(order);
		writeToBuffer(bf);
		bf.rewind();
		return bf;
	}
	
	public byte[] getByteArray(ByteOrder order) {
		return getByteBuffer(order).array();
	}
	
	public byte[] getByteArray(){
		return this.getByteArray(ByteTransfer.BYTEORDER);
	}
	/**
     * 转换buffer为对象
     * @param bf
     */
	public void parse(byte[] message, int offset, ByteOrder order) {
		//通过包装的方法创建的缓冲区保留了被包装数组内保存的数据
		ByteBuffer bf = ByteBuffer.wrap(message, offset, message.length - offset);
		bf.order(order);
		bf.rewind();
		readFromBuffer(bf);
	}

	/**
     * 转换buffer为对象
     * @param bf
     */
	public void parse(byte[] message, ByteOrder order) {
		parse(message, 0, order);
	}
	/**
     * 转换buffer为对象
     * @param bf
     */
	public void parse(byte[] message){
		this.parse(message, ByteTransfer.BYTEORDER);
	}
	
	public AbstractStruct(){}

}
